<?php

    // Author: 阿茵Ayin
    // Data: 2021.10.23 15:38

    require("util/SQLUtil.php");

    // Core class
    class SQLHelper {

        var $connection;

        public function __construct($username, $password, $serverName) {

            $this->connection = createSQLConnection($username, $password, $serverName);
        }

        public function __destruct() {

            closeSQLConnection($this->connection);
        }

        // Select a database
        public function selectDatabase($databaseName) {

            selectDB($this->connection, $databaseName);
        }

        // Execute a mysql command
        public function execSql($sql) {

            return mysqli_query($this->connection, $sql);
        }

        // Insert data into table
        // $tableName, ...Values
        public function insertInto() {

            $tableName = func_get_arg(0);
            $sql = "insert into " . $tableName . " (";
            for($i = 1; $i < func_num_args(); $i++) {

                $sql = $sql . "\"" . func_get_arg($i) . "\"";
                if($i != func_num_args() - 1) {
                    $sql = $sql . ", ";
                }
            }
            $sql = $sql . ")";
            echo($sql);

            return $this->execSql($sql);
        }

        // Return all data from a table
        public function selectAll($tableName) {
            
            $sql = "select * from " . $tableName;
            return $this->execSql($sql);
        }
    }

?>